<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ page contentType = "text/html; charset=UTF-8" %>

<html>
<head>

<c:set var="cssFileName">
	<c:out value="${crudViewMeta.cssFileName}" default="default.css"/>
</c:set>
<link rel="stylesheet" href="<c:url value='/css/${cssFileName}'/>" type="text/css">

<title><c:out value="${crudMeta.title}"/>数据编辑页面</title>

<script type="text/javascript">

function gotoListView()
{
	window.location="<c:url value='/list.do'><c:param name='name' value='${crudMeta.beanName}'/>
	<c:forEach items='${crudMeta.otherWantedParameters}' var='wantedParam'>
    	<c:param name='${wantedParam}' value='${param[wantedParam]}'/>
    </c:forEach></c:url>";
}

</script>

</head>

<body>

<table class="top" align="center">
	<tr><td class="headtitle">编辑<c:out value="${crudMeta.title}"/></td></tr>
</table>


<form name="listForm" action="<c:url value='/editPage.do'/>" method="POST"/>
	<input type="hidden" name="name" value="<c:out value='${crudMeta.beanName}'/>"/>
	<input type="hidden" name="selected" value="all"/>
	<c:forEach items="${crudMeta.otherWantedParameters}" var="wantedParam">
		<input type="hidden" name="<c:out value='${wantedParam}'/>" value="<c:out value='${param[wantedParam]}'/>">
	</c:forEach>
	
	<table class="content" width="<c:out value='${crudViewMeta.listTableWidth}'/>">
	
    <tr>
	  	<!--列名称-->
		<c:forEach items="${crudMeta.columnMetas}" var="col">
	  		<c:if test="${col.showInEditForm}">
	  			<td class="headtitle" nowrap><c:out value="${col.title}"/>
	  			<c:if test="${!col.nullable}"><span class="notNullHint">*</span></c:if>
	  			</td>
	  		</c:if>
	  	</c:forEach>
	</tr>
	
	<c:set var="nrOfElements" value="0"/>
	<c:set var="validateErrorIndex" value="-1"/>
	
	<c:forEach items="${command.records}" var="item" varStatus="s">
	<tr>
		<c:forEach items="${crudMeta.columnMetas}" var="col">
			
			<spring:bind path="command.records[${nrOfElements}].mapContent[${col.colName}]">
				<c:set var="errorPersent"><c:if test="${not empty status.errorMessage}">true</c:if></c:set>
				<c:if test="${not empty errorPersent}">
					<c:set var="validateErrorIndex"><c:out value="${validateErrorIndex + 1}"/></c:set>
				</c:if>
			</spring:bind>
			
			<c:if test="${!col.derived}">			
				<spring:bind path="command.records[${nrOfElements}].mapContentBefore[${col.colName}]">
					<input type="hidden" name="<c:out value='${status.expression}'/>" value="<c:out value='${status.value}'/>">
				</spring:bind>
			</c:if>
				
			<c:if test="${col.showInEditForm}">
				
				<!--如果该列是引申而来的，显示数据的同时也需要提供表单元素，否则在校验不通过的时候无法显示其值-->
				<c:if test="${col.derived}">
					<td class="listhead">
						<spring:bind path="command.records[${nrOfElements}].mapContent[${col.colName}]">
							<input type="hidden" name="<c:out value='${status.expression}'/>" value="<c:out value='${status.value}'/>">
						</spring:bind>
						<c:if test="${empty col.dictName || !col.needDictReplace}">
							<c:out value="${command.records[nrOfElements].mapContent[col.colName]}"/>
						</c:if>
						<c:if test="${not empty col.dictName && col.needDictReplace}">
							<c:out value="${col.dictMap[command.records[nrOfElements].mapContent[col.colName]]}"/>
						</c:if>	
					</td>
				</c:if>
				
				<c:if test="${!col.derived}">
					<c:if test="${!col.editable}">
					<td class="listhead">
					<spring:bind path="command.records[${nrOfElements}].mapContent[${col.colName}]">
						<input type="hidden" name="<c:out value='${status.expression}'/>" value="<c:out value='${status.value}'/>">
						<c:out value='${status.value}'/>
					</spring:bind>
					</td>
					</c:if>
		
					<c:if test="${col.editable && empty col.dictName}">
					<td>
					<spring:bind path="command.records[${nrOfElements}].mapContent[${col.colName}]">
						<input type="text" name="<c:out value='${status.expression}'/>" value="<c:out value='${status.value}'/>">
					</spring:bind>
					<c:if test="${not empty errorPersent}">
						<span class="validateError"><--&nbsp;<c:out value="${validateErrorIndex+1}"/></span>
					</c:if>
					</td>
		      		</c:if>
  		
	  				<c:if test="${col.editable && not empty col.dictName}">
	  				<td>
	  					<spring:bind path="command.records[${nrOfElements}].mapContent[${col.colName}]">
						<select name="<c:out value='${status.expression}'/>">
							<c:if test="${!col.primaryKey && col.nullable}"><option value="">(未选取)</option></c:if>
							<c:forEach var="map" items="${col.dictMap}">
							<option value='<c:out value="${map.key}"/>' <c:if test="${map.key == status.value}">selected="selected"</c:if>><c:out value="${map.value}"/></option>
							</c:forEach>
						</select>
						</spring:bind>
					<c:if test="${not empty errorPersent}">
						<span class="validateError"><--&nbsp;<c:out value="${validateErrorIndex+1}"/></span>
					</c:if>
					</td>
					</c:if>

				</c:if>
				
				
				
			</c:if>
			
			<c:if test="${!col.showInEditForm && !col.derived}">
				<spring:bind path="command.records[${nrOfElements}].mapContent[${col.colName}]">
					<input type="hidden" name="<c:out value='${status.expression}'/>" value="<c:out value='${status.value}'/>">
				</spring:bind>
			</c:if>
	
		</c:forEach>		
	</tr>
	<c:set var="nrOfElements"><c:out value="${nrOfElements + 1}"/></c:set>

	</c:forEach>
</table>

<br>
<table>
	<c:forEach items="${crudMeta.columnMetas}" var="col">
	<c:if test="${not empty col.hint}">
		<tr>
			<td><b><c:out value="${col.title}"/>：</b></td>
			<td><span class="hint"><c:out value="${col.hint}"/></span><td>
		</tr>
	</c:if>
	</c:forEach>
</table>

<br>
<c:if test="${validateErrorIndex >= 0}">
<table width="100%">
	<c:set var="nrOfElements" value="0"/>
	<c:set var="validateErrorIndex" value="-1"/>
	<c:forEach items="${command.records}" var="item" varStatus="s">
		<c:forEach items="${crudMeta.columnMetas}" var="col">			
			<spring:bind path="command.records[${nrOfElements}].mapContent[${col.colName}]">
				<c:set var="errorPersent"><c:if test="${not empty status.errorMessage}">true</c:if></c:set>
				<c:if test="${not empty errorPersent}">
					<c:set var="validateErrorIndex"><c:out value="${validateErrorIndex + 1}"/></c:set>
					<tr>
						<span class="validateError">
							<c:out value="${validateErrorIndex+1}"/>.&nbsp;&nbsp;
							<c:out value="${status.errorMessage}"/>
						</span>
					</tr>
				</c:if>
			</spring:bind>
		</c:forEach>
		<c:set var="nrOfElements"><c:out value="${nrOfElements + 1}"/></c:set>
	</c:forEach>
</table>
<br>
</c:if>

<input type="hidden" name="nrOfElements" value="<c:out value='${nrOfElements}'/>">

<table>
	<tr align="center" valign="center">
		<td align="left" width="50%">
			<input type="submit" name="save" value="保存">
			<input type="button" name="back" value="返回" onclick="javascript:gotoListView()">
		</td>
		<td width="50%">
		</td>
	</tr>
</table>
</form>
</body>
</html>